package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.adapter.LoanApplyListAdapter;
import com.zhiqianbao.entity.LoanApply;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;
import com.zhiqianbao.utils.WorthPurseUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2015/12/29.
 */
public class ApplyRecordHistoryActivity extends BaseActivity {
    TextView textViewHintHistory;
    /**
     * 历史申请记录列表
     */
    ListView listViewApplyHistoryRecord;

    Handler checkHistoriesHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RESULT_CANCELED :
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case RESULT_OK :
                    Result<List<Object>> data = (Result<List<Object>>) msg.obj;
                    if(data!=null) {
                        List<Object> loanApplies = data.getData();
                        if (loanApplies == null || loanApplies.size() == 0) {
                            textViewHintHistory.setVisibility(View.VISIBLE);
                            listViewApplyHistoryRecord.setVisibility(View.GONE);
                        } else {
                            textViewHintHistory.setVisibility(View.GONE);
                            listViewApplyHistoryRecord.setVisibility(View.VISIBLE);

                            List<LoanApply> list = new ArrayList<>();
                            for (Object loanApply : loanApplies) {
                                LoanApply la = gson.fromJson(gson.toJson(loanApply), LoanApply.class);
                                list.add(la);
                            }
                            listViewApplyHistoryRecord.setAdapter(new LoanApplyListAdapter(ApplyRecordHistoryActivity.this, list));
                        }
                    }
                    break;
            }
            closeWaitDialog();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_history_record);
        init();
        initView();
    }

    private void initView() {
       textViewHintHistory=(TextView)findViewById(R.id.textView_hint_history);
        listViewApplyHistoryRecord=(ListView)findViewById(R.id.listView_apply_history_record);
        if (Constants.LOGIN_USER == null) {
            Intent loginIntent = new Intent(ApplyRecordHistoryActivity.this,
                    WorthPurseUtil.getZhiddActivity().getClass());
            startActivity(loginIntent);
            finish();
        }
        else {
            //调用查询申请历史纪录
            showWaitDialog();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Map<String, String> params = new HashMap<>();
                    params.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                    params.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                    String result = HttpUtils.httpPostString(Constants.API_ROOT + "/loan/loanApplyHistories", params, ApplyRecordHistoryActivity.this);
                    Message message = new Message();
                    if (StringUtils.isBlank(result)) {
                        message.what = RESULT_CANCELED;
                    } else {
                        message.what = RESULT_OK;
                        Result<List<Object>> data = Result.fromJson(result, List.class);
                        message.obj = data;
                    }
                    checkHistoriesHandler.sendMessage(message);
                }
            }).start();
        }
    }
}
